package com.chenzhengrong.demospringboot.common.base;


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;

import static com.chenzhengrong.demospringboot.common.enums.ResultCode.*;


/**
 * @author M1nG
 * @version 1.0.0
 * @description 统一响应返回结果集
 * @date 2020-03-15 15:58
 */
@Data
@Builder
@AllArgsConstructor
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private String code;

    private String message;

    private T data;


    public Result() {
        super();
        this.code = SUCCESS.getCode();
        this.message = SUCCESS.getMessage();
    }

    public Result(String code, String message) {
        this.code = code;
        this.message = message;
        this.data = null;
    }

    public Result(T data) {
        this.data = data;
        this.code = SUCCESS.getCode();
        this.message = SUCCESS.getMessage();
    }

    public static Result success(Object data, String message) {
        return Result.builder()
                .code(SUCCESS.getCode())
                .message(message)
                .data(data)
                .build();
    }


    public static Result success(Object data) {
        return Result.builder()
                .code(SUCCESS.getCode())
                .message(SUCCESS.getMessage())
                .data(data)
                .build();
    }


    public static Result success() {
        return Result.builder()
                .code(SUCCESS.getCode())
                .message(SUCCESS.getMessage())
                .data("ok")
                .build();
    }

    public static Result fail() {
        return Result.builder()
                .code(FAILED.getCode())
                .message(FAILED.getMessage())
                .data("failed")
                .build();
    }


    public static Result fail(String message) {
        return Result.builder()
                .code(FAILED.getCode())
                .message(message)
                .data("failed")
                .build();
    }


    public static Result error() {
        return Result.builder()
                .code(INTERNAL_SERVER_ERROR.getCode())
                .message(INTERNAL_SERVER_ERROR.getMessage())
                .data("后台系统错误!")
                .build();
    }

    public static Result error(String message) {
        return Result.builder()
                .code(INTERNAL_SERVER_ERROR.getCode())
                .message(message)
                .data("后台系统错误!")
                .build();
    }

}
